<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class SendSmsList extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'SendSmsList';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '短信群发脚本';

    protected $apiCon = 'api';
    protected $log = 'SendAllSms'; //日志


    protected $errorLog = 'error_SendAllSms'; //错误日志

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        set_time_limit(0);
        //获取满足当日发送短信数据
        $count = DB::connection($this->apiCon)->table('sms_alls')->where("sendtime", strtotime(date('Y-m-d H:i', time())))->where('send_status', 0)
            ->count();
        if ($count == 0) {
            write_log($this->log, "当前暂无发送队列", '');
        } else {
            //计算页数
            $nums = 1000;//一次多少条
            $page = ceil($count / $nums);
            for ($i = 0; $i < $page; $i++) {
                //偏移量
                $limitset = (($i + 1) - 1) * $nums;
                //
                $datas = DB::connection($this->apiCon)->table('sms_alls')->where("sendtime", strtotime(date('Y-m-d H:i', time())))->where('send_status', 0)->offset($limitset)->limit($nums)->get();
                $datas = objToArray($datas);
                foreach ($datas as $v) {
                    DB::beginTransaction();
                    try {
                        //当前处理的数据id
                        $id = $v['id'];
                        //队列手机号
                        $phonelist = explode(',', $v['phone']);
                        $data['phonelist'] = $phonelist;
                        $data['content'] = $v['content'];
                        //调用第三方短信群发接口
                        $status = curl_request(getKkaudioApiUrl() . '/inner/sms/sendSms', $data, 'json', 'POST');
                        if ($status['code'] != 0) {
                            write_log($this->errorLog, "第三方发送返回失败", $status);
                            //更新当前短信状态
                            DB::connection($this->apiCon)->table('sms_alls')->where('id', $id)->update(['send_status' => 2,'msgId'=>0]);
                            //更新状态表
                            $faillist = $phonelist;
                            foreach ($faillist as $k => $v) {
                                //数组预处理
                                $insert[$k]['phone'] = $v;
                                $insert[$k]['receiveTime'] = date('Y-m-d H:i', time());
                                $insert[$k]['status'] = 'FAILURE';
                                $insert[$k]['smsid'] = $status['data'];
                            }
                            DB::connection($this->apiCon)->table('sms_all_log')->insert($insert);
                        } else {
                            //短信服务返回的msgid
                            $msgId = $status['data']['msgId'];
                            //更新已发送状态
                            DB::connection($this->apiCon)->table('sms_alls')->where('id',$id)->update(['send_status'=>1,'msgId'=>$msgId]);
                        }
                        DB::commit();
                        write_log($this->log, "群发短信id为" . $id . '发送成功', $status);
                    } catch (\Exception $e) {
                        DB::rollBack();
                        write_log($this->errorLog, "发送信息入库失败", $e->getMessage());
                    }
                }
            }
        }

    }

}
